<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
    <style>
        .account {
            width: 400px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div class="account">
    <h1 style="text-align: center;">注册</h1>
    <form method="post">
        {% for field in form %}
            {% if field.name == 'code' %}
                <div class="form-group">
                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                    <div class="clearfix">
                        <div class="col-md-6" style="padding-left: 0;">{{ field }}</div>
                        <div class="col-md-6"><input type="button" onclick="sms();sendemail()" id="btn" class="btn btn-default"
                                                     value="点击获取验证码"></div>
                    </div>

                </div>
            {% else %}
                <div class="form-group">
                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                    {{ field }}
                </div>
            {% endif %}
        {% endfor %}

        <button type="submit" class="btn btn-primary">注 册</button>
    </form>

</div>
<script src="/static/js/jquery-3.3.1.min.js"></script>
<script>
    function sms() {
        $.ajax({
            url: '/send/sms/',
            type: 'get',
            data: {
                'phone': $('#id_mobile_phone').val(),
                'tpl': 'register',
                'csrfmiddlewaretoken': $('[name="csrfmiddlewaretoken"]').val()
            },
            success: function (ret) {
                if (ret != '成功') {
                    alert(ret)
                }
            },
            error: function (ret) {
                console.log(ret)

            }


        })
    }
</script>

<script type="text/javascript">
var countdown=60;
function sendemail(){
    var obj = $("#btn");
    settime(obj);

    }
function settime(obj) { //发送验证码倒计时
    if (countdown == 0) {
        obj.attr('disabled',false);
        //obj.removeattr("disabled");
        obj.val("点击获取验证码");
        countdown = 60;
        return;
    } else {
        obj.attr('disabled',true);
        obj.val("重新发送(" + countdown + ")");
        countdown--;
    }
setTimeout(function() {
    settime(obj) }
    ,1000)
}
</script>


</body>
</html>